﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;
using SharedLibraries;
using ThangNM_LIB.FUNC;
using SharedLibraries.Constants;

namespace ThangNM.Admin
{
    public partial class AccessUI : System.Web.UI.Page
    {
        public FuncEO objFunc
        {
            get { return (FuncEO)ViewState["objFunc"]; }
            set { ViewState["objFunc"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                try
                {
                    if (Request.QueryString[QueryString.request] != null)
                    {
                        String key = Security.Login.Decrypt(Request.QueryString[QueryString.request], System.Configuration.ConfigurationManager.AppSettings["PRIVATE_KEY"]);
                        Int32 _PK_iFunctionID = 0;
                        if (Int32.TryParse(key, out _PK_iFunctionID))
                        {
                            objFunc = (new ShareDAO.FuncDAO()).SelectItem(_PK_iFunctionID);
                            if (!CommonsCheck.Object_OK(objFunc))
                            {
                                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Chức năng không tồn tại");
                                return;
                            }
                            else if (objFunc.bStatus == false)
                            {
                                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Chức năng đã bị khóa");
                                return;
                            }
                            else
                            {
                                key = Security.getTokenFunc(objFunc, true, objFunc.sToken);
                                if (String.IsNullOrEmpty(key))
                                {
                                    phMain.Controls.Add(Page.LoadControl(OtherEx.getAppSettings("RawUrl_UC", "~/usercontrol/") + objFunc.sPath));
                                    return;
                                }
                                else
                                {
                                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, key);
                                    return;
                                }

                            }
                        }
                        else
                        {
                            MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format("Dữ liệu nhập vào [{0}] không đúng định dạng số", key));
                            return;
                        }
                    }
                    else
                    {
                        objFunc = new FuncEO();
                        objFunc.sPath = "~/UserControl/Admin/AdminUC.ascx";
                        phMain.Controls.Add(Page.LoadControl(objFunc.sPath));
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format("Sai đường dẫn chức năng"));
                        return;
                    }
                }
                catch (Exception ex)
                {
                    phMain.Controls.Add(Page.LoadControl("~/UserControl/Admin/AdminUC.ascx"));
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
                }
                objFunc = null;
            }

            if (objFunc != null)
                phMain.Controls.Add(Page.LoadControl(OtherEx.getAppSettings("RawUrl_UC", "~/usercontrol/") + objFunc.sPath));
        }

        public void setClearMsg()
        {
            MsgUC1.setClearMsg();
        }

        public void setNotifications(Int16 iType, string strMsg, String strMsgFull = null)
        {
            MsgUC1.setNotifications(iType, "[ASPX]" + strMsg, strMsgFull);
        }
    }
}
//lblMsg.Text = OtherEx.LoadMenu(1, 0, 0);
//txtInput.Text = (new Security()).Check_Access(0);